package com.ei.itop.stat.controller;

import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ailk.dazzle.util.type.DateUtils;
import com.ailk.dazzle.util.type.StringUtils;
import com.ei.itop.common.bean.OpInfo;
import com.ei.itop.common.dbentity.CcCust;
import com.ei.itop.common.dbentity.ScOp;
import com.ei.itop.common.util.SessionUtil;
import com.ei.itop.custmgnt.service.CustMgntService;
import com.ei.itop.incidentmgnt.bean.QCIncident;
import com.ei.itop.scmgnt.service.OpService;
import com.ei.itop.stat.bean.IncidentAnalysisReport;
import com.ei.itop.stat.bean.IncidentReport;
import com.ei.itop.stat.bean.IncidentStateReport;
import com.ei.itop.stat.service.ReportService;

@Controller
@RequestMapping("/stat/report")
public class ReportController {
	
	private static final Logger log = Logger.getLogger(ReportController.class);

	@Autowired
	ReportService reportService;
	
	@Autowired
	CustMgntService custMgntService;
	
	@Autowired
	OpService opService;
	
	@Resource(name = "mailSendConfig")
	Map<String, String> mailSendConfig;
	
	/**
	 * 导出问题明细报表
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/exportTicketDetailReport")
	public void exportTicketDetailReport(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		OpInfo oi = SessionUtil.getOpInfo();
		// 构建查询条件实体
		QCIncident qi = new QCIncident();		
		// 责任顾问ID
		String adviserId = request.getParameter("expConsultantId");
		if (!StringUtils.isEmpty(adviserId)) {
			qi.setAdviserId(adviserId.split(","));
		}
		// 时间范围
		String expRegistStartDate = request.getParameter("expRegistStartDate");
		if (!StringUtils.isEmpty(expRegistStartDate)) {
			qi.setRegisterTimeBegin(DateUtils.string2Date(expRegistStartDate,
					DateUtils.FORMATTYPE_yyyy_MM_dd));
		}
		String expRegistEndDate = request.getParameter("expRegistEndDate");
		if (!StringUtils.isEmpty(expRegistEndDate)) {
			qi.setRegisterTimeEnd(DateUtils.dateOffset(DateUtils.string2Date(
					expRegistEndDate, DateUtils.FORMATTYPE_yyyy_MM_dd),
					Calendar.DAY_OF_YEAR, 1));
		}
		qi.setOrgId(oi.getOrgId());
		qi.setOrderByRegisterTime("DESC");
		// 查询顾问信息
		ScOp op = opService.queryScOp(Long.parseLong(adviserId));
		// 查询事件数据
		List<IncidentReport> datas = reportService
				.MBLQueryIncident4Report(qi, -1, 0, oi,SessionUtil.getLocale().toString());
		// 导出Excel文档
		InputStream in = null;
		try {
			in = this.getClass().getClassLoader()
					.getResourceAsStream("/export/TicketDetailStatReport.xlsx");
			Workbook workbook = new XSSFWorkbook(in);
			Sheet sheet = workbook.getSheetAt(0);
			String opName = op.getOpName()+"/"+op.getLastName()+"."+op.getFirstName();
			sheet.getRow(0).getCell(0).setCellValue(opName+"维护支持问题报告");
			String dateRange = expRegistStartDate+"~"+expRegistEndDate;
			sheet.getRow(1).getCell(0).setCellValue(dateRange);
			// 设置数据行单元格格式
			CellStyle style = workbook.createCellStyle();
			style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
			style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
			style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
			style.setBottomBorderColor(HSSFColor.BLACK.index);
			style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
			style.setLeftBorderColor(HSSFColor.BLACK.index);
			style.setBorderRight(HSSFCellStyle.BORDER_THIN);
			style.setRightBorderColor(HSSFColor.BLACK.index);
			style.setBorderTop(HSSFCellStyle.BORDER_THIN);
			style.setTopBorderColor(HSSFColor.BLACK.index);
			style.setWrapText(true);
			Font font = workbook.createFont();
			font.setFontName("Times New Roman");
			font.setFontHeightInPoints((short) 10);
			style.setFont(font);
			// 开始填充数据
			int startRow = 4;
			int no = 1;
			for (IncidentReport incident : datas) {
				Row row = sheet.createRow(startRow);
				int cellIndex = 0;
				// 序号
				Cell cellNo = row.createCell(cellIndex++);
				cellNo.setCellValue(no++);
				cellNo.setCellStyle(style);
				// 客户
				Cell cellCustomer = row.createCell(cellIndex++);
				cellCustomer.setCellValue(incident.getTopCustName());
				cellCustomer.setCellStyle(style);
				// 事件序列号
				Cell cellCode = row.createCell(cellIndex++);
				cellCode.setCellValue(incident.getIncidentCode());
				cellCode.setCellStyle(style);
				// 登记时间
				Cell cellRegisterDate = row.createCell(cellIndex++);
				cellRegisterDate.setCellValue(DateUtils.date2String(incident.getRegisteTime(),
										DateUtils.FORMATTYPE_yyyy_MM_dd_HH_mm_ss));
				cellRegisterDate.setCellStyle(style);
				// 登记人
				Cell cellRegister = row.createCell(cellIndex++);
				cellRegister.setCellValue(incident.getPlObjectName());
				cellRegister.setCellStyle(style);
				// 公司
				Cell cellCompany = row.createCell(cellIndex++);
				cellCompany.setCellValue(incident.getCustName());
				cellCompany.setCellStyle(style);
				// 描述
				Cell cellDesc = row.createCell(cellIndex++);
				cellDesc.setCellValue(incident.getBrief());
				cellDesc.setCellStyle(style);
				// 事件分类
				Cell cellClass = row.createCell(cellIndex++);
				cellClass.setCellValue(incident.getClassVal());
				cellClass.setCellStyle(style);
				// 模块
				Cell cellModule = row.createCell(cellIndex++);
				cellModule.setCellValue(incident.getModuleName());
				cellModule.setCellStyle(style);
				// 影响度
				Cell cellAffect = row.createCell(cellIndex++);
				cellAffect.setCellValue(incident.getAffectVal());
				cellAffect.setCellStyle(style);
				// 优先级
				Cell cellPriority = row.createCell(cellIndex++);
				cellPriority.setCellValue(incident.getPriorityVal());
				cellPriority.setCellStyle(style);
				// 根本原因
				Cell cellRootcause = row.createCell(cellIndex++);
				cellRootcause.setCellValue(incident.getIncidentReason());
				cellRootcause.setCellStyle(style);
				// 解决方案
				Cell cellSolution = row.createCell(cellIndex++);
				cellSolution.setCellValue(incident.getItSolution());
				cellSolution.setCellStyle(style);
				// 工时
				Cell cellTime = row.createCell(cellIndex++);
				String dealDur = incident.getDealDur()==null?"":String.valueOf(Double.valueOf(incident.getDealDur())/3600);
				cellTime.setCellValue(dealDur);
				cellTime.setCellStyle(style);
				// 状态
				Cell cellState = row.createCell(cellIndex++);
				cellState.setCellValue(incident.getItStateVal());
				cellState.setCellStyle(style);
				// 计划完成时间
				Cell cellPlanFinishDate = row.createCell(cellIndex++);
				cellPlanFinishDate
						.setCellValue(incident.getDealDur2() == null ? ""
								: DateUtils.date2String(
										incident.getDealDur2(),
										DateUtils.FORMATTYPE_yyyy_MM_dd_HH_mm_ss));
				cellPlanFinishDate.setCellStyle(style);
				// 实际完成时间
				Cell cellActualFinishDate = row.createCell(cellIndex++);
				cellActualFinishDate
						.setCellValue(incident.getFinishTime() == null ? ""
								: DateUtils.date2String(
										incident.getFinishTime(),
										DateUtils.FORMATTYPE_yyyy_MM_dd_HH_mm_ss));
				cellActualFinishDate.setCellStyle(style);
				// 问题报告
				Cell cellReport = row.createCell(cellIndex++);
				if(StringUtils.isEmpty(incident.getReportAttachName())){
					cellReport.setCellValue("");
					cellReport.setCellStyle(style);
				}else{
					CellStyle linkStyle = workbook.createCellStyle();  
					Font linkFont = workbook.createFont();  
					linkFont.setUnderline(Font.U_SINGLE);  
					linkFont.setColor(IndexedColors.BLUE.getIndex());
					linkStyle.setFont(linkFont);
					linkStyle.setWrapText(true);
					Hyperlink reportLink = workbook.getCreationHelper().createHyperlink(Hyperlink.LINK_URL);  
					reportLink.setAddress(mailSendConfig.get("mail.website")+"/incident/downloadReport?incidentId="+incident.getIcIncidentId());
					cellReport.setHyperlink(reportLink);
					cellReport.setCellValue(incident.getReportAttachName());
					cellReport.setCellStyle(linkStyle);
				}
				//知识库文档
				Cell cellKnowledge = row.createCell(cellIndex++);
				cellKnowledge.setCellValue("");
				cellKnowledge.setCellStyle(style);
				// 归档标记
				String archiveFlag = incident.getArchiveFlag();
				if (StringUtils.isEmpty(archiveFlag)) {
					// 重复问题
					Cell cellRepeating = row.createCell(cellIndex++);
					cellRepeating.setCellValue("");
					cellRepeating.setCellStyle(style);
				} else {
					// 重复问题
					Cell cellRepeating = row.createCell(cellIndex++);
					cellRepeating
							.setCellValue(archiveFlag.charAt(2) == '0' ? "No"
									: "Yes");
					cellRepeating.setCellStyle(style);
				}
				// 满意度
				Cell cellSatis = row.createCell(cellIndex++);
				cellSatis.setCellValue(incident.getFeedbackVal());
				cellSatis.setCellStyle(style);
				startRow++;
			}
			response.setContentType("application/octet-stream");
			response.addHeader(
					"Content-Disposition",
					"attachment;filename="
							+ new String(("ams-report[" + dateRange + "]("+opName+").xlsx")
									.getBytes("gb2312"), "ISO8859-1"));
			workbook.write(response.getOutputStream());
		} catch (Exception e) {
			log.error("", e);
			response.getWriter()
					.print("<script type='text/javascript'>alert('"+SessionUtil.getRequestContext().getMessage("i18n.incident.export.error")+"')</script>");
		} finally {
			if (in != null) {
				in.close();
			}
		}
	}
	
	
	
	/**
	 * 导出问题汇总报表(周统计)
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/exportTicketSumReport")
	public void exportTicketSumReport(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		OpInfo oi = SessionUtil.getOpInfo();	
		// 责任顾问ID
		String adviserId = request.getParameter("expConsultantId");
		// 时间范围
		Date expStartDate = DateUtils.string2Date(request.getParameter("expStartDate"),
				DateUtils.FORMATTYPE_yyyy_MM_dd);
		Date expEndDate = DateUtils.string2Date(request.getParameter("expEndDate"),
				DateUtils.FORMATTYPE_yyyy_MM_dd);
		// 查询顾问信息
		ScOp op = opService.queryScOp(Long.parseLong(adviserId));
		// 查询报表数据
		List<IncidentStateReport> datas = reportService
				.MBLQueryIncidentSumReport(adviserId,expStartDate,expEndDate);
		// 导出Excel文档
		InputStream in = null;
		try {
			in = this.getClass().getClassLoader()
					.getResourceAsStream("/export/TicketStateSumStatReport.xlsx");
			Workbook workbook = new XSSFWorkbook(in);
			Sheet sheet = workbook.getSheetAt(0);
			String opName = op.getOpName()+"/"+op.getLastName()+"."+op.getFirstName();
			sheet.getRow(0).getCell(0).setCellValue(opName+"维护支持问题报告");
			String dateRange = request.getParameter("expStartDate")+"~"+request.getParameter("expEndDate");
			sheet.getRow(1).getCell(0).setCellValue(dateRange);
			// 设置数据行单元格格式
			CellStyle style = workbook.createCellStyle();
			style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
			style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
			style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
			style.setBottomBorderColor(HSSFColor.BLACK.index);
			style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
			style.setLeftBorderColor(HSSFColor.BLACK.index);
			style.setBorderRight(HSSFCellStyle.BORDER_THIN);
			style.setRightBorderColor(HSSFColor.BLACK.index);
			style.setBorderTop(HSSFCellStyle.BORDER_THIN);
			style.setTopBorderColor(HSSFColor.BLACK.index);
			style.setWrapText(true);
			Font font = workbook.createFont();
			font.setFontName("宋体");
			font.setFontHeightInPoints((short) 12);
			style.setFont(font);
			// 开始填充数据
			int startRow = 4;
			for (IncidentStateReport sr : datas) {
				Row row = sheet.createRow(startRow++);
				int cellIndex = 0;
				Cell curCell = null;
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getCustName()==null?"":sr.getCustName());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getBeginUnSolvedCount1()==null?0:sr.getBeginUnSolvedCount1());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getBeginUnSolvedCount2()==null?0:sr.getBeginUnSolvedCount2());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getBeginUnSolvedCount3()==null?0:sr.getBeginUnSolvedCount3());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getBeginUnSolvedCountX()==null?0:sr.getBeginUnSolvedCountX());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getBeginUnSolvedSumCount()==null?0:sr.getBeginUnSolvedSumCount());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getRecCount1()==null?0:sr.getRecCount1());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getRecCount2()==null?0:sr.getRecCount2());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getRecCount3()==null?0:sr.getRecCount3());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getRecCountX()==null?0:sr.getRecCountX());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getRecSumCount()==null?0:sr.getRecSumCount());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getSolvedCount1()==null?0:sr.getSolvedCount1());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getSolvedCount2()==null?0:sr.getSolvedCount2());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getSolvedCount3()==null?0:sr.getSolvedCount3());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getSolvedCountX()==null?0:sr.getSolvedCountX());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getSolvedSumCount()==null?0:sr.getSolvedSumCount());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getEndUnsolvedCount1()==null?0:sr.getEndUnsolvedCount1());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getEndUnsolvedCount2()==null?0:sr.getEndUnsolvedCount2());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getEndUnsolvedCount3()==null?0:sr.getEndUnsolvedCount3());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getEndUnsolvedCountX()==null?0:sr.getEndUnsolvedCountX());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getEndUnsolvedSumCount()==null?0:sr.getEndUnsolvedSumCount());
				curCell.setCellStyle(style);
			}			
			response.setContentType("application/octet-stream");
			response.addHeader(
					"Content-Disposition",
					"attachment;filename="
							+ new String(("ConsultantWeekReport[" + dateRange + "]("+opName+").xlsx")
									.getBytes("gb2312"), "ISO8859-1"));
			workbook.write(response.getOutputStream());
		} catch (Exception e) {
			log.error("", e);
			response.getWriter()
					.print("<script type='text/javascript'>alert('"+SessionUtil.getRequestContext().getMessage("i18n.incident.export.error")+"')</script>");
		} finally {
			if (in != null) {
				in.close();
			}
		}
	}
	/**
	 * 导出问题分析报表
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/exportTicketAnalysisReport")
	public void exportTicketAnalysisReport(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		OpInfo oi = SessionUtil.getOpInfo();	
		// 客户ID
		long custId = Long.parseLong(request.getParameter("expCustId"));
		CcCust cust = custMgntService.getCustInfo(custId);
		// 时间范围
		String expStartDateStr = request.getParameter("expStartDate");
		String expEndDateStr = request.getParameter("expEndDate");
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		Date expStartDate = df.parse(expStartDateStr+"01");
		Date expEndDate = DateUtils.dateOffset(df.parse(expEndDateStr+"01"), Calendar.MONTH, 1);
		List<IncidentAnalysisReport> mainData = reportService.MBLQueryIncidentAnalysisGroupByType(custId,expStartDate,expEndDate);
		//写入excel
		InputStream in = null;
		try {
			in = this.getClass().getClassLoader()
					.getResourceAsStream("/export/TicketCustomerStatReport.xlsx");
			Workbook workbook = new XSSFWorkbook(in);
			Sheet sheet = workbook.getSheetAt(0);
			sheet.getRow(0).getCell(0).setCellValue(cust.getCustName()+" "+expStartDateStr+"-"+expEndDateStr+"问题分析表");
			// 设置数据行单元格格式
			CellStyle style = workbook.createCellStyle();
			style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
			style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
			style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
			style.setBottomBorderColor(HSSFColor.BLACK.index);
			style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
			style.setLeftBorderColor(HSSFColor.BLACK.index);
			style.setBorderRight(HSSFCellStyle.BORDER_THIN);
			style.setRightBorderColor(HSSFColor.BLACK.index);
			style.setBorderTop(HSSFCellStyle.BORDER_THIN);
			style.setTopBorderColor(HSSFColor.BLACK.index);
			style.setWrapText(true);
			Font font = workbook.createFont();
			font.setFontName("宋体");
			font.setFontHeightInPoints((short) 12);
			style.setFont(font);
			CellStyle boldStyle = workbook.createCellStyle();
			boldStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
			boldStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
			boldStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
			boldStyle.setBottomBorderColor(HSSFColor.BLACK.index);
			boldStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
			boldStyle.setLeftBorderColor(HSSFColor.BLACK.index);
			boldStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
			boldStyle.setRightBorderColor(HSSFColor.BLACK.index);
			boldStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
			boldStyle.setTopBorderColor(HSSFColor.BLACK.index);
			boldStyle.setWrapText(true);
			Font font2 = workbook.createFont();
			font2.setFontName("宋体");
			font2.setFontHeightInPoints((short) 12);
			font2.setBoldweight(Font.BOLDWEIGHT_BOLD);
			boldStyle.setFont(font2);
			CellStyle noBorderStyle = workbook.createCellStyle();
			noBorderStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
			noBorderStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
			noBorderStyle.setWrapText(true);
			noBorderStyle.setFont(font);
			// 开始填充数据
			int startRow = 2;
			for (int i=0;i<mainData.size()-1;i++) {
				IncidentAnalysisReport sr = mainData.get(i);
				Row row = sheet.createRow(startRow++);
				int cellIndex = 0;
				Cell curCell = null;
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getIncidentType());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getProdTypeCount1()==null?"":(sr.getProdTypeCount1()+""));
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getProdTypeCount2()==null?"":(sr.getProdTypeCount2()+""));
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getProdTypeCount3()==null?"":(sr.getProdTypeCount3()+""));
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getProdTypeCount4()==null?"":(sr.getProdTypeCount4()+""));
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getProdTypeCount5()==null?"":(sr.getProdTypeCount5()+""));
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getProdTypeSumCount());
				curCell.setCellStyle(style);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getPercent()==null?"":(sr.getPercent()+"%"));
				curCell.setCellStyle(boldStyle);
			}
			if(mainData.size()>1){
				//表格最后一行百分比
				IncidentAnalysisReport sr = mainData.get(mainData.size()-1);
				Row row = sheet.createRow(startRow++);
				int cellIndex = 0;
				Cell curCell = null;
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue("");
				curCell.setCellStyle(boldStyle);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getProdTypeCount1()==null?"0%":(sr.getProdTypeCount1()+"%"));
				curCell.setCellStyle(boldStyle);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getProdTypeCount2()==null?"0%":(sr.getProdTypeCount2()+"%"));
				curCell.setCellStyle(boldStyle);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getProdTypeCount3()==null?"0%":(sr.getProdTypeCount3()+"%"));
				curCell.setCellStyle(boldStyle);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getProdTypeCount4()==null?"0%":(sr.getProdTypeCount4()+"%"));
				curCell.setCellStyle(boldStyle);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(sr.getProdTypeCount5()==null?"0%":(sr.getProdTypeCount5()+"%"));
				curCell.setCellStyle(boldStyle);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue("");
				curCell.setCellStyle(boldStyle);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue("100%");
				curCell.setCellStyle(boldStyle);
				//隔一行，开始下面的数据填充
				startRow++;
				//填充状态汇总及满意度汇总
				Map<Integer,Integer> solveStateMap = reportService.MBLQueryIncidentSolveStateCount(custId, expStartDate, expEndDate);
				Map<Integer,Integer> satisfyStateMap = reportService.MBLQueryIncidentSatisfyStateCount(custId, expStartDate, expEndDate);
				row = sheet.createRow(startRow++);
				cellIndex = 0;
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue("已解决：");
				curCell.setCellStyle(noBorderStyle);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(solveStateMap.get(1)==null?0:solveStateMap.get(1));
				curCell.setCellStyle(noBorderStyle);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue("十分满意：");
				curCell.setCellStyle(noBorderStyle);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(satisfyStateMap.get(1)==null?0:satisfyStateMap.get(1));
				curCell.setCellStyle(noBorderStyle);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue("满意：");
				curCell.setCellStyle(noBorderStyle);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(satisfyStateMap.get(2)==null?0:satisfyStateMap.get(2));
				curCell.setCellStyle(noBorderStyle);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue("不满意：");
				curCell.setCellStyle(noBorderStyle);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(satisfyStateMap.get(3)==null?0:satisfyStateMap.get(3));
				curCell.setCellStyle(noBorderStyle);
				row = sheet.createRow(startRow++);
				cellIndex = 0;
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue("挂起：");
				curCell.setCellStyle(noBorderStyle);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(solveStateMap.get(2)==null?0:solveStateMap.get(2));
				curCell.setCellStyle(noBorderStyle);
				curCell = row.createCell(cellIndex++);
				row = sheet.createRow(startRow++);
				cellIndex = 0;
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue("处理中：");
				curCell.setCellStyle(noBorderStyle);
				curCell = row.createCell(cellIndex++);
				curCell.setCellValue(solveStateMap.get(3)==null?0:solveStateMap.get(3));
				curCell.setCellStyle(noBorderStyle);
			}
			response.setContentType("application/octet-stream");
			response.addHeader(
					"Content-Disposition",
					"attachment;filename="
							+ new String(("CustomerTicketStatics["+expStartDateStr+"-"+expEndDateStr+"]("+cust.getCustName()+").xlsx")
									.getBytes("gb2312"), "ISO8859-1"));
			workbook.write(response.getOutputStream());
		} catch (Exception e) {
			log.error("", e);
			response.getWriter()
					.print("<script type='text/javascript'>alert('"+SessionUtil.getRequestContext().getMessage("i18n.incident.export.error")+"')</script>");
		} finally {
			if (in != null) {
				in.close();
			}
		}
		
	}
	
	
}
